package com.prs.crm.dao.cdxh.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.DistributionOrderDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;

public class DistributionOrderDaoImpl extends BaseDaoImpl<DistributionOrder> implements DistributionOrderDao {

	@Override
	public void saveOrUpdate(DistributionOrder distributionOrder) {
		// TODO Auto-generated method stub
	
			this.getHibernateTemplate().saveOrUpdate(distributionOrder);
	}

	@Override
	public DistributionOrder getDistribution(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(DistributionOrder.class,id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DistributionOrder getDistributionOrder(final String disOrderCode,
			final String disOrderCustomer) {
		return (DistributionOrder)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrder.class);
				        	criteria.add(Restrictions.eq("disOrderCode", disOrderCode));
				        	criteria.add(Restrictions.eq("disOrderCustomer", disOrderCustomer));				        	
				        	List<DistributionOrder> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	@Override
	public void saveOrDistributionOrder(DistributionOrder distributionOrder) {
		this.getHibernateTemplate().saveOrUpdate(distributionOrder);
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DistributionOrderDetail getDistributionOrderDetail(
			final String productCode,final DistributionOrder distributionOrder) {
		return (DistributionOrderDetail)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrderDetail.class);
				        	criteria.add(Restrictions.eq("productCode", productCode));
				        	criteria.add(Restrictions.eq("distributionOrder", distributionOrder));				        	
				        	List<DistributionOrderDetail> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}

	@Override
	public void saveOrDistributionOrderDetail(
			DistributionOrderDetail distributionOrderDetail) {
		this.getHibernateTemplate().saveOrUpdate(distributionOrderDetail);
		

}

	@Override
	public DistributionOrderDetail getDistributionOrderDetail(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(DistributionOrderDetail.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DistributionOrder setDistributionOrder(final String saleOrderPlan) {
		return (DistributionOrder)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(DistributionOrder.class);
				        	criteria.add(Restrictions.eq("disOrderCode", saleOrderPlan));			        				        	
				        	List<DistributionOrder> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}}
